package com.smec.remes.module.scan.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.smec.moblie.R;
import com.smec.moblie.databinding.ItemAllocMaterBinding;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.module.scan.beans.SmecAllocMateriel;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by xupeizuo on 2018/3/28.
 */

public class SmecAllocAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private Context context;
    private ArrayList<SmecAllocMateriel> arrayList=new ArrayList<>();

    public static final int VIEW_DATA=10002;
    public static final int VIEW_NULL=10001;

    public SmecAllocAdapter(Context context) {
        this.context = context;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == VIEW_NULL){
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }

        ItemAllocMaterBinding itemAllocMaterBinding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_alloc_mater,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemAllocMaterBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(itemAllocMaterBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == VIEW_DATA){
            ItemAllocMaterBinding itemAllocMaterBinding=(ItemAllocMaterBinding)holder.getBinding();
            itemAllocMaterBinding.setAllocItem(arrayList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setArrayList(ArrayList<SmecAllocMateriel> arrayList) {
        if(CommonUtils.notEmpty(arrayList)){
            this.arrayList = arrayList;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(arrayList == null || arrayList.size() == 0){
            return VIEW_NULL;
        }else {
            return VIEW_DATA;
        }
    }
}
